-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
variables/options
should be required if TVariables
is not empty/default
#11241
base: main
Are you sure you want to change the base?
Conversation
… is empty or default
🦋 Changeset detectedLatest commit: 08793f5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
...[options]: HasRequiredVariables< | ||
TVariables, | ||
[ | ||
optionsWithVariables: QueryHookOptionsWithVariables< | ||
NoInfer<TData>, | ||
NoInfer<TVariables> | ||
>, | ||
], | ||
[options?: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>] | ||
> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have decided to use the "one signature, tuple behind conditional approach" here, as staying with only one public overload will make errors more readable and "localized".
This is how an error would look like if we had two overloads - the full call is marked red and the error is hard to read:
Instead, using this approach, the error looks like this - only variables
are highlighted and the error is much more readable:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also something to highlight in these screenshots: the interface name QueryHooksOptionsWithVariables
is a lot clearer than something calculated inline, like QueryHookOptions<...> & { variables: TVariables }
useQuery(query, { | ||
variables: { | ||
// @ts-expect-error on unknown variable | ||
foo: "bar", | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indenting these tests like this to highlight where exactly I am expecting the error to be propagated to the user.
@@ -9,7 +9,7 @@ export function Query< | |||
TVariables extends OperationVariables = OperationVariables, | |||
>(props: QueryComponentOptions<TData, TVariables>) { | |||
const { children, query, ...options } = props; | |||
const result = useQuery(query, options); | |||
const result = useQuery<unknown, OperationVariables>(query, options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where this causes problems is in generic implementations like our Query
HOC:
Similar errors would always appear if we go through with the "options/variables sometimes required" approach here - there is no way of implementing that without that result.
Luckily, as you can see there's an easy fix - but this might cause some minor churn.
size-limit report 📦
|
variables/options
should only be required if TVariables
is empty or defaultvariables/options
should be required if TVariables
is not empty/default
c80ba36
to
3f7eecb
Compare
@phryneas is this one ready to go and just needing a review? |
This would address #10857
I'm opening this PR now to get feedback on it - if we agree that this is the way forward, it would need to be done for all other hooks as well - maybe we can find a nice type helper for it, although the implicitly created interface with a name helps for autocomplete readability.
I would target 3.9 with this - I don't expect this to negatively impact anyone, but it should probably be in a patch release.
Checklist: